package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.entity.House;
import pers.cj.pdos.mapper.HouseMapper;
import pers.cj.pdos.mapper.OrderRuleMapper;
import pers.cj.pdos.service.IHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.HouseVo;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {
    @Autowired
    HouseMapper houseMapper;
    @Autowired
    OrderRuleMapper orderRuleMapper;
    @Override
    public List<HouseVo> listVo(String companyId) {
        return houseMapper.listVo(null,null,companyId,null,null,null);
    }

    @Override
    public List<HouseVo> listNormalVo(String companyId,String storeId) {
        return houseMapper.listVo(null,null,companyId,storeId,null,1);
    }

    @Override
    public List<HouseVo> listVoPage(Integer page, Integer limit, String companyId, String searchStoreId, String searchContent, Integer status) {
        return houseMapper.listVo(Utils.genPage(page,limit),limit,companyId,searchStoreId, Utils.genLikeData(searchContent),status);
    }

    @Override
    public long count(String companyId, String searchStoreId, String searchContent, Integer status) {
        return houseMapper.count(companyId,searchStoreId, Utils.genLikeData(searchContent),status);
    }

    @Override
    public boolean checkName(String name, String companyId) {
        return houseMapper.selectCount(new QueryWrapper<House>().eq("name",name).eq("company_id",companyId)) > 0;
    }

    @Override
    public boolean checkNo(String no, String companyId) {
        return houseMapper.selectCount(new QueryWrapper<House>().eq("no",no).eq("company_id",companyId)) > 0;
    }

    @Override
    public boolean check(String id) {
        return Utils.check(houseMapper.check(id));
    }

    @Override
    public boolean checkLock(String id) {
        return houseMapper.selectCount(new QueryWrapper<House>().eq("id",id).eq("locking",true)) > 0;
    }

    @Override
    public HouseVo getVoById(String id) {
        return houseMapper.getVoById(id);
    }

    @Override
    public House getById(String id) {
        return houseMapper.selectById(id);
    }

    @Override
    public List<House> listNormalByStore(String storeId) {
        return houseMapper.selectList(new QueryWrapper<House>().eq("store_id",storeId).eq("status",1));
    }

    @Override
    public void insert(House house) {
        if (house.getDef()){
            //如果是默认仓库，则本门店其他仓库为非默认
            House temp = new House();
            temp.setDef(false);
            houseMapper.update(temp, new QueryWrapper<House>().eq("store_id",house.getStoreId()));
        }
        houseMapper.insert(house);
    }

    @Override
    public void remove(House house) {
        houseMapper.deleteById(house.getId());
    }

    @Override
    public void update(House house) {
        if (house.getDef()){
            //如果是默认仓库，则本门店其他仓库为非默认
            House temp = new House();
            temp.setDef(false);
            houseMapper.update(temp, new QueryWrapper<House>().eq("store_id",house.getStoreId()));
        }
        houseMapper.updateById(house);
    }

    @Override
    public House getDefault(String storeId) {
        List<House> list = houseMapper.selectList(new QueryWrapper<House>().eq("status","1").eq("def",1).eq("store_id",storeId));
        if (Utils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<House> list(String companyId) {
        return houseMapper.selectList(new QueryWrapper<House>().eq("company_id",companyId));
    }
}
